﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace TextFileSplitter
{
    class Program
    {
        static void Main(string[] args)
        {
            // Test only, create test data
            if (args.Length > 0 && string.Compare(args[0], "/create", true) == 0)
            {
                int count = 0;

                if (args.Length > 1)
                    int.TryParse(args[1], out count);

                if (count == 0)
                    count = 100000;

                StringBuilder builder = new StringBuilder();

                for (int i = 0; i < count; i++)
                {
                    builder.AppendFormat("{0} {1}\r\n", i, Guid.NewGuid());
                }

                File.AppendAllText(Path.GetRandomFileName() + ".txt", builder.ToString());
                return;
            }

            // Help
            if (args.Length < 2 || args[0] == "/?")
            {
                Console.WriteLine("Usage: ");
                Console.WriteLine("{0} {1}", Path.GetFileName(Assembly.GetEntryAssembly().Location), "path size [/byline]");
                Console.WriteLine("path: The file to be splitted.");
                Console.WriteLine("size: The size(in bytes) of each splitted file. e.g. 1024 represents 1k.");
                Console.WriteLine("/byline: Spliit by line, This option may cause the actual size of each splitted file smaller than the specified size. ");
                return;
            }

            // Split
            int size;

            if (int.TryParse(args[1], out size))
            {
                new Splitter(args[0]).Split(size, args.Length > 2 ? string.Compare(args[2], "/byline") == 0 : false);
            }
        }
    }
}
